#include "http_request.h"

using comm::http::custorm::CHttpRequest;

const uint32_t CHttpRequest::DEFAULT_TIMEOUT;
const uint32_t CHttpRequest::DEFAULT_MAX_REDIRECT;

CHttpRequest::CHttpRequest(void) :
    m_dwTimeout(DEFAULT_TIMEOUT), m_bSSLVerify(true),
    m_bRedirect(false), m_dwMaxRedirect(DEFAULT_MAX_REDIRECT)
{
}

void CHttpRequest::SetMainUrl(const std::string& sUrl)
{
    m_sMainUrl = sUrl;
}

std::string CHttpRequest::GetFullUrl(void) const
{
    std::string sFullUrl(m_sMainUrl);
	std::map<std::string, std::string>::const_iterator iter    = m_mapHttpParams.begin();
	std::map<std::string, std::string>::const_iterator iterEnd = m_mapHttpParams.end();
	for (; iter != iterEnd; iter++)
	{
		if (sFullUrl.find('?') != std::string::npos)
		{
			sFullUrl.append("?");
		}
		else
		{
			sFullUrl.append("&");
		}
		
		sFullUrl.append(iter->first);
		sFullUrl.append("=");
		sFullUrl.append(iter->second);
	}

	return sFullUrl;
}

const void CHttpRequest::GetAllParameter(std::map<std::string, std::string>& mapHttpParameters) const
{
    mapHttpParameters.clear();
	mapHttpParameters = m_mapHttpParams;
}

void CHttpRequest::SetParameter(
    const std::string& sName, const std::string& sValue)
{
    m_mapHttpParams[sName] = sValue;
}

bool CHttpRequest::HasParameter() const
{
	return !m_mapHttpParams.empty();
}

void CHttpRequest::AddHeader(
    const std::string& sName, const std::string& sValue)
{
    m_mmapHttpHeaders.insert(std::pair<std::string, std::string>(sName, sValue));
}


void CHttpRequest::GetAllHeaders(std::multimap<std::string, std::string>& mmapHttpHeaders) const
{
    mmapHttpHeaders.clear();
	mmapHttpHeaders = m_mmapHttpHeaders;
}


bool CHttpRequest::HasHeader() const
{
    return !m_mmapHttpHeaders.empty();
}

void CHttpRequest::SetTimeout(uint32_t dwTimeout)
{
    m_dwTimeout = dwTimeout;
}

uint32_t CHttpRequest::GetTimeout(void) const
{
    return m_dwTimeout;
}

void CHttpRequest::EnableSSLVerify(void)
{
    m_bSSLVerify = true;
}

void CHttpRequest::DisableSSLVerify(void)
{
    m_bSSLVerify = false;
}

bool CHttpRequest::IsSSLVerifyEnabled(void) const
{
    return m_bSSLVerify;
}

void CHttpRequest::EnableRedirect(void)
{
    m_bRedirect = true;
}

void CHttpRequest::DisableRedirect(void)
{
    m_bRedirect = false;
}

bool CHttpRequest::IsRedirectEnabled(void) const
{
    return m_bRedirect;
}

void CHttpRequest::SetMaxRedirect(uint32_t dwMaxRedirect)
{
    m_dwMaxRedirect = dwMaxRedirect;
}

uint32_t CHttpRequest::GetMaxRedirect(void) const
{
    return m_dwMaxRedirect;
}
